package com.xiaoxiong.mami.web;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xiaoxiong.mami.dao.daomain.Goods;
import com.xiaoxiong.mami.dao.daomain.GoodsProperties;
import com.xiaoxiong.mami.http.AppJsonResult;
import com.xiaoxiong.mami.service.GoodsPropertiesService;
import com.xiaoxiong.mami.service.GoodsService;
import com.xiaoxiong.mami.utils.UUIDUtils;
import com.xiaoxiong.mami.vo.Paginate;
import io.swagger.annotations.Api;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by Administrator on 2017/7/26.
 */
@RestController
@RequestMapping("/goods")
@Api("库存")
public class GoodsWeb {

    Logger logger = Logger.getLogger(getClass());

    @Autowired
    GoodsService goodsService;

    @Autowired
    GoodsPropertiesService goodsPropertiesService;

    @RequestMapping(value = "/getGoods", method = RequestMethod.POST)
    public JSONObject getGoods(@RequestParam String type) {
        logger.info("=====type======" + type);
        List<Goods> goodsList = goodsService.getGoodsList();
        JSONArray goodsListJson = new JSONArray();
        if (goodsList!=null){
            goodsListJson = (JSONArray) JSONArray.toJSON(goodsList);
        }
        JSONObject result = new JSONObject();
        for (int i = 0; i < goodsListJson.size(); i++) {
            List<GoodsProperties> goodsProperties = goodsPropertiesService.getGoodsPropertiesById(goodsListJson.getJSONObject(i).getString("goodsId"));
            if (goodsProperties != null) {
                JSONArray goodsPropertiesJsonArray = (JSONArray) JSONArray.toJSON(goodsProperties);
                goodsListJson.getJSONObject(i).put("goodsProperties", goodsPropertiesJsonArray);
            }
        }
        result.put("goodsList",goodsListJson);
        return AppJsonResult.createSuccessResultStr(result);
    }

    @RequestMapping(value = "/addGoods", method = RequestMethod.PUT)
    public JSONObject addGoods(@RequestParam(required = false) String title, @RequestParam(required = false) Double price,
                               @RequestParam(required = false) String goodsDesc, @RequestParam(required = false) String images,
                               @RequestParam(required = false) String thumbUrl, @RequestParam(required = false) Integer sort,
                               @RequestParam(required = false) String status, @RequestParam(required = false) String goodsImages,
                               @RequestParam(required = false) Integer stock, @RequestParam(required = false) String setMeal,
                               @RequestParam(required = false) Integer buyNumMax, @RequestParam(required = false) String goodsNumber) {

        String goodsId = UUIDUtils.createUUID();
        int result = goodsService.insertGood(goodsId, title, price, goodsDesc, images, thumbUrl, sort, status, goodsImages, stock, setMeal, buyNumMax, goodsNumber);
        if (result == 1){
            return AppJsonResult.createSuccessResultMsg("添加成功");
        }
        return AppJsonResult.createErrorResultStr("添加失败");
    }

    @RequestMapping(value = "/updateGoods/{userId}", method = RequestMethod.POST)
    public JSONObject updateGoods(@PathVariable String userId,@RequestParam() String goodsId, @RequestParam(required = false) String title, @RequestParam(required = false) Double price,
                                  @RequestParam(required = false) String goodsDesc, @RequestParam(required = false) String images,
                                  @RequestParam(required = false) String thumbUrl, @RequestParam(required = false) Integer sort,
                                  @RequestParam(required = false) String status, @RequestParam(required = false) String goodsImages,
                                  @RequestParam(required = false) Integer stock, @RequestParam(required = false) String setMeal,
                                  @RequestParam(required = false) Integer buyNumMax, @RequestParam(required = false) String goodsNumber) {

        logger.info("==== GoodsID ====: " + goodsId);

        int result = goodsService.updateGoods(goodsId, title, price, goodsDesc, images, thumbUrl, sort, status, goodsImages, stock, setMeal, buyNumMax, goodsNumber);
        if (result == 1){
            return AppJsonResult.createSuccessResultMsg("更新成功");
        }
        return AppJsonResult.createErrorResultStr("更新失败");
    }

    @RequestMapping(value = "/getGoodsById/{goodsId}", method = RequestMethod.GET)
    public JSONObject getGoodsById(@PathVariable("goodsId") String goodsId) {
        Goods goods = goodsService.getGoodsById(goodsId);
        JSONObject goodsJson = new JSONObject();
        if (goods != null) {
            goodsJson = (JSONObject) JSONObject.toJSON(goods);
            String imagesStr = goods.getImages();
            goodsJson.put("images", toStringArrayByStr(imagesStr));
            imagesStr = goods.getGoodsImages();
            goodsJson.put("nodesImages", toStringArrayByStr(imagesStr));
        }
        JSONObject.toJSON(goods);
        JSONObject result = AppJsonResult.createSuccessResultStr(goodsJson);
        logger.info("======getGoodsById======" + result);
        return result;
    }

    private JSONArray toStringArrayByStr(String imagesStr) {
        JSONArray imagesJson = new JSONArray();
        if (StringUtils.isEmpty(imagesStr)) {
            return imagesJson;
        }
        String[] images = imagesStr.split(",");
        if (images != null) {
            for (String item : images) {
                imagesJson.add(item);
            }
        }
        return imagesJson;
    }


}
